﻿unit PmRanksService;

interface
{$I ../Config.inc}
uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm,
  PmRanksModel,
  PmRanksBLL,
  PmItemDataModel;
  
type


  TPmRanksService = class(TPmRanksBLL)
  private

  protected

  public
  {$IFDEF SERVER}
    function BeforeTreatment(const iItem:TPmRanks; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeTreatment(const iItems:TPmRanksList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeCheck(const iItem:TPmRanks; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeCheck(const iItems:TPmRanksList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function Check(const iItem:TPmRanks; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function Check(const iItems:TPmRanksList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeExecute(const iItem:TPmRanks; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeExecute(const iItems:TPmRanksList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function AfterTreatment(const iItem:TPmRanks; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function AfterTreatment(const iItems:TPmRanksList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function CalculateSortNo(out oNewSortNo:Integer; out oError:string):Boolean;
  {$ENDIF}
    function LoadAll(const iItems:TPmRanksList; out oError:string; const iJustUse:Boolean=False):Boolean;
    function Use(const iItem:TPmRanks; out oError:string):Boolean;
  end;
  
implementation

{$IFDEF SERVER}
function TPmRanksService.BeforeTreatment(const iItem:TPmRanks; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  ItemDB:TPmRanks;
  NewSortNo:Integer;
begin
  if iItem<>nil then
  begin
    ItemDB:=TPmRanks.Create;
    try
      case iPreMode of
      pmAdd:
        begin
          if not CalculateSortNo(NewSortNo,oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
          iItem.RanksIsSystem:=False;
          iItem.RanksStatus:=1;
          iItem.RanksSortNo:=NewSortNo;
          iItem.RecordDate:=Now;
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItem.ClassName)) then
          begin
            if ItemDB.LoadFromStr(iItem.SaveToStr,oError) and
              Reload(ItemDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItem.ClassName),ItemDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItem.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;

          iItem.ModifyDate:=Now;
        end;
      pmDelete:
        begin
          if not Reload(iItem,oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      end;
    finally
      FreeAndNil(ItemDB);
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeTreatment错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmRanksService.BeforeTreatment(const iItems:TPmRanksList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmRanksService.BeforeCheck(const iItem:TPmRanks; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmRanksService.BeforeCheck(const iItems:TPmRanksList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmRanksService.Check(const iItem:TPmRanks; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  ItemDB:TPmRanks;
  SQL,SQLResultStr:string;
begin
  if iItem<>nil then
  begin
    ItemDB:=TPmRanks.Create;
    try
      if SameStr(Trim(iItem.RanksId),EmptyStr) then
      begin
        oError:='物料等级ID不允许为空';
        Result:=False;
        Exit;
      end else if SameStr(Trim(iItem.RanksName),EmptyStr) then
      begin
        oError:='物料等级名称不允许为空';
        Result:=False;
        Exit;
      end else if iItem.RanksIsSystem then
      begin
        oError:='不允许创建、修改、删除系统级物料等级';
        Result:=False;
        Exit;
      end else if (iItem.RanksStatus<0) or (iItem.RanksStatus>1) then
      begin
        oError:='物料等级状态非法';
        Result:=False;
        Exit;
      end else if SameStr(Trim(iItem.OrgId),EmptyStr) then
      begin
        oError:='归属组织不允许为空';
        Result:=False;
        Exit;
      end;

      case iPreMode of
      pmAdd:
        begin
          if SameStr(Trim(iItem.RecordPsn),EmptyStr) then
          begin
            oError:='录入人不允许为空';
            Result:=False;
            Exit;
          end;
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItem.ClassName)) then
          begin
            if ItemDB.LoadFromStr(iItem.SaveToStr,oError) and
              Reload(ItemDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItem.ClassName),ItemDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItem.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
          if SameStr(Trim(iItem.ModifyPsn),EmptyStr) then
          begin
            oError:='物料等级人不允许为空';
            Result:=False;
            Exit;
          end else if iItem.RanksId<>ItemDB.RanksId then
          begin
            oError:='物料等级ID不允许修改';
            Result:=False;
            Exit;
          end else if iItem.RanksIsSystem<>ItemDB.RanksIsSystem then
          begin
            oError:='物料等级类型不允许修改';
            Result:=False;
            Exit;
          end else if not SameStr(Trim(iItem.RecordPsn),Trim(ItemDB.RecordPsn)) then
          begin
            oError:='录入人不允许修改';
            Result:=False;
            Exit;
          end else if not SameStr(Trim(iItem.OrgId),Trim(ItemDB.OrgId)) then
          begin
            oError:='归属组织不允许修改';
            Result:=False;
            Exit;
          end else if not SameDateTime(iItem.RecordDate,ItemDB.RecordDate) then
          begin
            oError:='录入日期不允许修改';
            Result:=False;
            Exit;
          end else if iItem.TimeStamp<>ItemDB.TimeStamp then
          begin
            oError:='物料等级信息已被他人修改，请重新装载...';
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
          SQL:=Format(string('SELECT COUNT(%s) FROM %s WHERE %s=''%s'' '),
                    [TPmItemDataModel.cRanksIdKey,TPmItemDataModel.cTblName,
                    TPmItemDataModel.cRanksIdKey,iItem.RanksId]);
          if TSK0001Comm.ReadSimpleData(fDBConn,fDBID,SQL,SQLResultStr,oError) then
          begin
            if StrToInt(SQLResultStr)>0 then
            begin
              oError:=Format(string('调用%s.Check错误...错误内容:当前物料等级已绑定物料,请先解除物料关系...'),[Self.ClassName]);
              Result:=False;
              Exit;
            end;
          end else begin
            oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;

          if iItem.RanksStatus<>0 then
          begin
            oError:='只允许删除已冻结物料等级';
            Result:=False;
            Exit;
          end
        end;
      end;
    finally
      FreeAndNil(ItemDB);
    end;
  end else begin
    oError:=Format(string('调用%s.Check错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmRanksService.Check(const iItems:TPmRanksList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmRanksService.BeforeExecute(const iItem:TPmRanks; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  if iItem<>nil then
  begin
    case iPreMode of
    pmAdd:
      begin
        iItem.TimeStamp:=TSK0001Comm.GetTimeStampMs;
      end;
    pmEdit:
      begin
        iItem.TimeStamp:=TSK0001Comm.GetTimeStampMs;
      end;
    pmDelete:
      begin
      end;
    pmUse:
      begin
      end;
    pmUnUse:
      begin
      end;
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeExecute错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmRanksService.BeforeExecute(const iItems:TPmRanksList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmRanksService.AfterTreatment(const iItem:TPmRanks; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmRanksService.AfterTreatment(const iItems:TPmRanksList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmRanksService.CalculateSortNo(out oNewSortNo:Integer; out oError:string):Boolean;
var
  SQL,MaxSortNoStr:string;
begin
  SQL:=Format(string('SELECT MAX(%s) AS %s FROM %s ;'),
              [TPmRanksModel.cRanksSortNoKey,TPmRanksModel.cRanksSortNoKey,
              TPmRanksModel.cTblName]);
  if TSK0001Comm.ReadSimpleData(fDBConn,EmptyStr,SQL,MaxSortNoStr,oError) then
  begin
    if SameStr(Trim(MaxSortNoStr),EmptyStr) then
    begin
      oNewSortNo:=1;
    end else begin
      oNewSortNo:=StrToInt(MaxSortNoStr)+1;
    end;
  end else begin
    oError:=Format(string('调用%s.CalculateSortNo方法失败, 错误内容:获取最大SortNo失败!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;
{$ENDIF}

function TPmRanksService.LoadAll(const iItems:TPmRanksList; out oError:string; const iJustUse:Boolean):Boolean;
var
  Condition,OrderBy:string;
begin
  if (iItems<>nil) then
  begin
    if iJustUse then
    begin
      Condition:=Format(string('%s=1'),[TPmRanksModel.cRanksStatusKey]);
    end else begin
      Condition:=EmptyStr;
    end;
    OrderBy:=TPmRanksModel.cRanksSortNoKey+' ASC';

    if not LoadByCondition(iItems,Condition,oError,OrderBy) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用TPmRanksService.LoadAll方法失败！错误内容:传入的iItems为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmRanksService.Use(const iItem:TPmRanks; out oError:string):Boolean;
begin
  if (iItem<>nil) then
  begin
    if iItem.RanksStatus=1 then
    begin
      iItem.RanksStatus:=0;
      if not inherited Edit(iItem,oError) then
      begin
        oError:=Format(string('TPmRanksService.Use方法失败！错误内容:%s'),[oError]);
        Result:=False;
        Exit;
      end;
    end else begin
      iItem.RanksStatus:=1;
      if not inherited Edit(iItem,oError) then
      begin
        oError:=Format(string('TPmRanksService.Use方法失败！错误内容:%s'),[oError]);
        Result:=False;
        Exit;
      end;
    end;
  end else begin
    oError:=Format(string('TPmRanksService.Use方法失败！错误内容:传入的iItem为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;
  
end.
